package com.example.administrator.nurse.present;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.nurse.R;
import com.example.administrator.nurse.base.BaseActivity;
import com.example.administrator.nurse.base.BaseFragment;
import com.example.administrator.nurse.fragment.LiquorDoConfigurationFragment;
import com.example.administrator.nurse.fragment.LiquorDoProofreadFragment;
import com.example.administrator.nurse.fragment.LiquorNoConfigurationFragment;
import com.example.administrator.nurse.fragment.LiquorNoProofreadFragment;
import com.example.administrator.nurse.widget.ChartTableView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huhaichao on 2017/1/12.
 */

public class LiquorManageActivity extends BaseActivity{
    private static int Flag = 0; // 标志位，标记当前操作数据的 fragment
    private ChartTableView chartTableView;
    private ArrayList<VisitSummaryData> dataList;
    private View selectTmp;
    private List<BaseFragment> fragmentList;
    private int currentIndex=0;
    LiquorNoConfigurationFragment liquorNoConfigurationFragment;
    LiquorDoConfigurationFragment liquorDoConfigurationFragment;
    LiquorNoProofreadFragment liquorNoProofreadFragment;
    LiquorDoProofreadFragment liquorDoProofreadFragment;


    private String[] flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        configCommonHeader(LiquorManageActivity.this, "配液管理", 1);
        //chartTableView = (ChartTableView) findViewById(R.id.chartTableView_visitSummary);
        ininView();
    }

    @Override
    protected int setLayotId() {
        return R.layout.liquor_manage_main;
    }

    @Override
    protected void onFailure(int requestCode, Object object, String errorMsg) {

    }

    @Override
    protected void onSuccess(int requestCode, Object object, String response) {

    }
    private void ininView(){
       /* dataList = new ArrayList<>();
        dataList.add(new VisitSummaryData("刘某",23,35,"2017.2.30","牛奶",100));
        dataList.add(new VisitSummaryData("萌萌",22,42,"2017.2.30","牛奶",100));
        dataList.add(new VisitSummaryData("秀秀",14,24,"2017.2.30","牛奶",100));
        dataList.add(new VisitSummaryData("光光",66,99,"2017.2.30","牛奶",100));
        dataList.add(new VisitSummaryData("瑞瑞",42,34,"2017.2.30","牛奶",100));
        dataList.add(new VisitSummaryData("美美",24,44,"2017.2.30","牛奶",100));
        dataList.add(new VisitSummaryData("笑笑",24,77,"2017.2.30","牛奶",100));
        dataList.add(new VisitSummaryData("狒狒",87,69,"2017.2.30","牛奶",100));
        dataList.add(new VisitSummaryData("飞飞",97,45,"2017.2.30","牛奶",100));
        dataList.add(new VisitSummaryData("肥肥",69,38,"2017.2.30","牛奶",100));
        dataList.add(new VisitSummaryData("小明",28,68,"2017.2.30","牛奶",100));
        dataList.add(new VisitSummaryData("小红",27,34,"2017.2.30","牛奶",100));*/
        //将数据传入自定义控件里
       // chartTableView.setDataList(dataList);

        LinearLayout container = (LinearLayout) f(R.id.container);

        View room= LayoutInflater.from(LiquorManageActivity.this).inflate(R.layout.select_item,(ViewGroup) getWindow().getDecorView(),false);
        ((TextView)room.findViewById(R.id.text)).setText("待配液");
        ((TextView)room.findViewById(R.id.text)).setTextColor(getResources().getColor(R.color.navbar));
        selectTmp=room;
        room.findViewById(R.id.line).setVisibility(View.VISIBLE);

        View patient= LayoutInflater.from(LiquorManageActivity.this).inflate(R.layout.select_item,(ViewGroup)getWindow().getDecorView(),false);
        ((TextView)patient.findViewById(R.id.text)).setText("待校对");

        View outpatient= LayoutInflater.from(LiquorManageActivity.this).inflate(R.layout.select_item,(ViewGroup)getWindow().getDecorView(),false);
        ((TextView)outpatient.findViewById(R.id.text)).setText("已配液");

        View do_proofread= LayoutInflater.from(LiquorManageActivity.this).inflate(R.layout.select_item,(ViewGroup)getWindow().getDecorView(),false);
        ((TextView)do_proofread.findViewById(R.id.text)).setText("已校对");

        container.addView(room);
        container.addView(patient);
        container.addView(outpatient);
        container.addView(do_proofread);

        onItemListener(room, patient, outpatient,do_proofread);

        // 下面开始配置 fragment，注意并不是，一次性全部实例化，下面 commit 的时候才会
        fragmentList=new ArrayList<BaseFragment>();
        liquorNoConfigurationFragment = new LiquorNoConfigurationFragment();// 待配液
        liquorDoConfigurationFragment = new LiquorDoConfigurationFragment();//已配液
        liquorNoProofreadFragment = new LiquorNoProofreadFragment();//待校对
        liquorDoProofreadFragment = new LiquorDoProofreadFragment();//已校对

        fragmentList.add(liquorNoConfigurationFragment);
        fragmentList.add(liquorNoProofreadFragment);
        fragmentList.add(liquorDoConfigurationFragment);
        fragmentList.add(liquorDoProofreadFragment);

        flag=new String[]{"tag2","tag1","tag3","tag4"};

        // 下面 是 首次进入的显示
        //changeShowStatus(true,true,false) ;
        getSupportFragmentManager().beginTransaction()
                .add(R.id.liquor_container, liquorNoConfigurationFragment, flag[0]).commit();
    }

    private void onItemListener(final View ... views){
        for (int i=0;i<views.length;i++){
            final View view=views[i];
            final int selectI=i; // 0 , 1 , 2, 3
            views[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (selectTmp!=null){
                        ((TextView)selectTmp.findViewById(R.id.text)).setTextColor(getResources().getColor(R.color.gray));
                        selectTmp.findViewById(R.id.line).setVisibility(View.GONE);
                    }
                    selectTmp=view;
                    ((TextView)view.findViewById(R.id.text)).setTextColor(getResources().getColor(R.color.navbar));
                    view.findViewById(R.id.line).setVisibility(View.VISIBLE);
                    switchFragment(selectI);
                    currentIndex=selectI;
                }
            });
        }
    }
    /** 切换页面时不需要重新加载，只需要重新匹配，重新匹配的方法是 adapter.notifyDataSetChanged() */
    private void switchFragment(int i){ // 如果从 0 点 1，那么 i = 1 , currentIndex =0
        BaseFragment tofragment   = (BaseFragment) getSupportFragmentManager().findFragmentByTag(flag[i]);
        BaseFragment fromfragment = (BaseFragment) getSupportFragmentManager().findFragmentByTag(flag[currentIndex]);
        replaceFragment(tofragment,fromfragment,i);
//        if (tofragment==null){
//            getSupportFragmentManager().beginTransaction().hide(fromfragment).add(R.id.patient_container, fragmentList.get(i), flag[i]).commit();
//        }else {
//            /** 下面这句不注释会引发一个 罕见的 异常 */
//            //getSupportFragmentManager().beginTransaction().hide(fromfragment).show(tofragment).commit();
//        }
        /** 下面不用再手动调用 adapter.notifyDataSetChanged() 会空指针，因为每个 f 在上面 add 的时候就实例化了*/
        try {
            switch (i) {
                case 0:
                    //changeShowStatus(true, true, false);
                    LiquorManageActivity.Flag = 0;
                    //patientRoomFragment.adapter.notifyDataSetChanged();
                    break;// 按病房
                case 1:
                    //changeShowStatus(true, true, true);
                    LiquorManageActivity.Flag = 1;
                    //patientAtRoomFragment.adapter.notifyDataSetChanged();
                    break;// 按病人
                case 2:
                    //changeShowStatus(false, false, true);
                    Log.d("zzzzz","--------按出院病人");
                    LiquorManageActivity.Flag = 2;
                    //patientAtOutFragment.adapter.notifyDataSetChanged();
                    break;// 按出院病人
                case 3:
                    //changeShowStatus(false, false, true);
                    Log.d("zzzzz","--------按出院病人");
                    LiquorManageActivity.Flag = 3;
                    //patientAtOutFragment.adapter.notifyDataSetChanged();
                    break;// 按出院病人
            }
        }catch(Exception e){
            Log.d("zzzzz",e.toString());
        }
    }

    private void replaceFragment(Fragment newFragment, Fragment fromFragment, int i) {

        android.support.v4.app.FragmentTransaction trasection = getSupportFragmentManager().beginTransaction();
        if (newFragment==null){
            getSupportFragmentManager().beginTransaction().hide(fromFragment).add(R.id.liquor_container, fragmentList.get(i), flag[i]).commit();
        }else{
            if(!newFragment.isAdded()) {
                try {
                    trasection.replace(R.id.liquor_container, newFragment);
                    trasection.addToBackStack(null);
                    trasection.commit();

                } catch (Exception e) {
                    // TODO: handle exception
                    //AppConstants.printLog(e.getMessage());

                }
            }else {
                //.hide(fromFragment)
                trasection.show(newFragment).commit();
            }
        }
    }
}
